package cn.datawin.task;

import java.util.ArrayList;
import java.util.List;


import cn.datawin.adsl.Adsl;
import cn.datawin.spider.CCMan;
import cn.datawin.spider.scheduler.QueueThreadPool;

public class Statistic {
	private int count=0;
	private List<Task> tasks=new ArrayList<Task>();
	private CCMan ccMan;
	private PrepareTask prepareTask;
	private int c=Integer.parseInt(Config.apps.get("count"));//每抓取多少条切换ip
	private  static Statistic statistic=null;
	
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public static Statistic getInstance() {  
	       if (statistic == null) {  
	    	   statistic = new Statistic();  
	       }  
	       return statistic;  
	    }  
	
	public void addCount(Task task) throws Exception {
		tasks.remove(task);
		count++;
		concut();
	}
	
	public List<Task> getTasks() {
		return tasks;
	}
	public void setTasks(List<Task> tasks1) {
		tasks.addAll(tasks1);
	}
	public CCMan getCcMan() {
		return ccMan;
	}
	public void setCcMan(CCMan ccMan) {
		this.ccMan = ccMan;
	}
	public PrepareTask getPrepareTask() {
		return prepareTask;
	}
	public void setPrepareTask(PrepareTask prepareTask) {
		this.prepareTask = prepareTask;
	}
	
	public void concut() throws Exception{
		if(count!=0 && count%c==0){
			this.prepareTask.setStop(true);
			this.prepareTask.pageQueue.clear();
			this.ccMan.setStop(true);
			this.ccMan.getQueueThreadPool().shutdown();
//			System.out.println("断网");
//			Thread.sleep(1000);
//			System.out.println("连接");
			Adsl.cutAdsl(Config.apps.get("adsltitle"));
			Thread.sleep(2000);
			Adsl.connAdsl(Config.apps.get("adsltitle"), Config.apps.get("adslname"), Config.apps.get("adslpass"));
			this.prepareTask.setTasks(tasks);
			this.prepareTask.setStop(false);
			this.prepareTask.start();
			this.ccMan.setPageQueue(this.prepareTask.pageQueue);
			this.ccMan.setQueueThreadPool(new QueueThreadPool(10, 10, 100));
			this.ccMan.setStop(false);
			this.ccMan.start();
			
		}
	}
}
